package net.sk.china.front.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import net.sk.china.common.response.CommonResult;
import net.sk.china.front.request.order.*;
import net.sk.china.front.response.order.*;
import net.sk.china.front.service.order.StoreOrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/12 20:51
 * +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("order/")
@Api(tags = "订单")
public class StoreOrderController {

    private final StoreOrderService orderService;

    public StoreOrderController(StoreOrderService orderService) {
        this.orderService = orderService;
    }

    @ApiOperation(value = "预下单")
    @RequestMapping(value = "pre/order", method = RequestMethod.POST)
    public CommonResult<Map<String, Object>> preOrder(@RequestBody @Validated PreOrderRequest request) {
        return CommonResult.success(orderService.preOrder(request));
    }

    @ApiOperation(value = "加载预下单")
    @RequestMapping(value = "load/pre/{preOrderNo}", method = RequestMethod.GET)
    public CommonResult<PreOrderResponse> loadPreOrder(@PathVariable String preOrderNo) {
        return CommonResult.success(orderService.loadPreOrder(preOrderNo));
    }

    @ApiOperation(value = "计算订单价格")
    @RequestMapping(value = "computed/price", method = RequestMethod.POST)
    public CommonResult<ComputedOrderPriceResponse> computedPrice(@Validated @RequestBody OrderComputedPriceRequest request) {
        return CommonResult.success(orderService.computedOrderPrice(request));
    }

    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public CommonResult<Map<String, String>> createOrder(@Validated @RequestBody CreateOrderRequest orderRequest) {
        return CommonResult.success(orderService.createOrder(orderRequest));
    }

    @ApiOperation(value = "订单列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "0=未支付,1=待发货,2=待收货,3=待评价,4=已完成,-3=售后/退款", required = true, dataTypeClass = Integer.class)
    })
    public CommonResult<CommonPage<OrderDetailResponse>> orderList(@RequestParam(name = "status") Integer status,
                                                                   @ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(orderService.list(status, pageRequest));
    }

    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "detail/{orderId}", method = RequestMethod.GET)
    public CommonResult<StoreOrderDetailInfoResponse> orderDetail(@PathVariable String orderId) {
        return CommonResult.success(orderService.detailOrder(orderId));
    }

    @ApiOperation(value = "订单头部数量")
    @RequestMapping(value = "data", method = RequestMethod.GET)
    public CommonResult<OrderDataResponse> orderData() {
        return CommonResult.success(orderService.orderData());
    }

    @ApiOperation(value = "删除订单")
    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    public CommonResult<Boolean> delete(@PathVariable Integer id) {
        if(orderService.delete(id)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "评价订单")
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    public CommonResult<Boolean> comment(@RequestBody @Validated StoreProductReplyAddRequest request) {
        if(orderService.reply(request)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "订单收货")
    @RequestMapping(value = "take", method = RequestMethod.POST)
    public CommonResult<Boolean> take(@RequestParam(value = "id") Integer id) {
        if(orderService.take(id)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "订单取消")
    @RequestMapping(value = "cancel", method = RequestMethod.POST)
    public CommonResult<Boolean> cancel(@RequestParam(value = "id") Integer id) {
        if(orderService.cancel(id)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "获取申请订单退款信息")
    @RequestMapping(value = "apply/refund/{orderId}", method = RequestMethod.GET)
    public CommonResult<ApplyRefundOrderInfoResponse> refundApplyOrder(@PathVariable String orderId) {
        return CommonResult.success(orderService.applyRefundOrderInfo(orderId));
    }

    @ApiOperation(value = "订单退款申请")
    @RequestMapping(value = "refund", method = RequestMethod.POST)
    public CommonResult<Boolean> refundApply(@RequestBody @Validated OrderRefundApplyRequest request) {
        if(orderService.refundApply(request)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "查询退款理由")
    @RequestMapping(value = "refund/reason", method = RequestMethod.GET)
    public CommonResult<List<String>> refundReason() {
        return CommonResult.success(orderService.getRefundReason());
    }

    @ApiOperation(value = "待评价商品信息查询")
    @RequestMapping(value = "product", method = RequestMethod.POST)
    public CommonResult<OrderProductReplyResponse> getOrderProductForReply(@Validated @RequestBody GetProductReplyRequest request) {
        return CommonResult.success(orderService.getReplyProduct(request));
    }

    @ApiOperation(value = "获取支付配置")
    @RequestMapping(value = "get/pay/config", method = RequestMethod.GET)
    public CommonResult<PreOrderResponse> getPayConfig() {
        return CommonResult.success(orderService.getPayConfig());
    }
}
